package com.org.forex;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.JCheckBox;
import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.SegmentedTimeline;
import org.jfree.chart.axis.Timeline;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.date.DateUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A demo showing a candlestick chart.
 * 
 */
public class CandlestickDemo extends ApplicationFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * A demonstration application showing a candlestick chart.
	 * 
	 * @param title
	 *            the frame title.
	 */

	public CandlestickDemo(final String title, Price[] prices, int start_index,
			int end_index, boolean scale_day) {

		super(title);

		final DefaultHighLowDataset dataset = createHighLowDataset(prices,
				start_index, end_index, scale_day);

		final JFreeChart chart = createChart(dataset);

		chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		setContentPane(chartPanel);

	}

	public static double getLowestLow(DefaultHighLowDataset dataset) {
		double lowest;
		lowest = dataset.getLowValue(0, 0);
		for (int i = 1; i < dataset.getItemCount(0); i++) {
			if (dataset.getLowValue(0, i) < lowest) {
				lowest = dataset.getLowValue(0, i);
			}
		}

		return lowest;
	}

	public static double getHighestHigh(DefaultHighLowDataset dataset) {
		double highest;
		highest = dataset.getHighValue(0, 0);
		for (int i = 1; i < dataset.getItemCount(0); i++) {
			if (dataset.getLowValue(0, i) > highest) {
				highest = dataset.getHighValue(0, i);
			}
		}

		return highest;
	}

	public static DefaultHighLowDataset createHighLowDataset(Price[] prices,
			int start_index, int end_index, boolean scale_day) {
		int range = end_index - start_index;
		Date[] date = new Date[range];
		double[] high = new double[range];
		double[] low = new double[range];
		double[] open = new double[range];
		double[] close = new double[range];
		double[] volume = new double[range];

		int weekday_beetween = 0;
		for (int i = 0; i < range; i++) {
			int index = i + start_index;
			if (i >= 1 && scale_day) {
				long diff_day = (prices[index].getDate().getTime() - prices[index - 1]
						.getDate().getTime()) / (1000 * 60 * 60 * 24);
				weekday_beetween += diff_day;

				Calendar cal = Calendar.getInstance();
				cal.setTime(prices[index].getDate());
				cal.add(Calendar.DATE, -weekday_beetween);
				date[i] = cal.getTime();

			} else
				date[i] = prices[index].getDate();

			high[i] = prices[index].getValue_high();
			low[i] = prices[index].getValue_low();
			open[i] = prices[index].getValue_open();
			close[i] = prices[index].getValue_close();
			volume[i] = prices[index].getVolume();
			// System.out.println(prices[index]);
		}

		return new DefaultHighLowDataset("Series 1", date, high, low, open,
				close, volume);

	}

	/**
	 * Creates a chart.
	 * 
	 * @param dataset
	 *            the dataset.
	 * 
	 * @return The dataset.
	 */
	public static JFreeChart createChart(final DefaultHighLowDataset dataset) {
		final JFreeChart chart = ChartFactory.createCandlestickChart(
				"Candlestick Demo", "Time", "Value", dataset, true);

		XYPlot plot = chart.getXYPlot();
		

		final DateAxis domainAxis = new DateAxis("Date");
		CandlestickRenderer renderer = new CandlestickRenderer();
		
		plot.setRenderer(renderer);
		plot.setDomainAxis(domainAxis);
		
		renderer.setSeriesPaint(0, Color.BLACK);
        renderer.setDrawVolume(true);
        final Timeline oldTimeline = domainAxis.getTimeline();        
        domainAxis.setTimeline(oldTimeline);       
        domainAxis.setAutoRange(true);
        

		// renderer.setBaseShapesVisible(true);
		NumberFormat currency = NumberFormat.getCurrencyInstance();
		currency.setMaximumFractionDigits(4);
		currency.setMinimumFractionDigits(4);
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setNumberFormatOverride(currency);

        rangeAxis.setAutoRangeIncludesZero(true);

		double lowestLow = getLowestLow(dataset);
		double highestHigh = getHighestHigh(dataset);

		rangeAxis.setRange(lowestLow * 1 - (highestHigh - lowestLow) * 0.3,
				highestHigh * 1 + (highestHigh - lowestLow) * 0.3);
		// System.out.println(lowestLow);
		// System.out.println(highestHigh);

		return chart;
	}

	public static void displayChart(String title, Price[] prices, int index,
			int size, boolean scaleday) {
		final CandlestickDemo demo = new CandlestickDemo(title, prices, index,
				index + size, scaleday);
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);
	}

}
